package cn.schoolwow.util.domain.query.instance;

import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.QuickFlowBuilder;
import cn.schoolwow.util.module.query.instance.domain.InstanceQueryOption;
import cn.schoolwow.util.module.query.instance.service.SetInstanceFieldValueFlow;

public class QueryInstance {
    private InstanceQueryOption instanceQueryOption = new InstanceQueryOption();

    private static QuickFlow quickFlow = QuickFlowBuilder.newInstance();

    /**创建实例查询*/
    public static QueryInstance newQuery(Object instance){
        QueryInstance instanceQuery = new QueryInstance();
        instanceQuery.instanceQueryOption.instance = instance;
        return instanceQuery;
    }

    public QueryInstance() {
        quickFlow.putContextThreadLocalInstanceData(instanceQueryOption);
    }

    /**是否忽略不存在的字段*/
    public QueryInstance ignoreNotExistField(boolean ignoreNotExistField){
        instanceQueryOption.ignoreNotExistField = ignoreNotExistField;
        return this;
    }

    /**设置字段的值*/
    public QueryInstance setFieldValue(String fieldName, Object fieldValue){
        quickFlow.startFlow(new SetInstanceFieldValueFlow())
                .putCurrentCompositeFlowData("fieldName", fieldName)
                .putCurrentCompositeFlowData("fieldValue", fieldValue)
                .execute();
        return this;
    }

    public QueryInstanceResponse execute(){
        return new QueryInstanceResponse(quickFlow);
    }
}
